import 'package:flutter/material.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'package:left_scroll_actions/left_scroll_actions.dart';
import 'package:provider/provider.dart';
import 'package:zhaoxiaowu_app/main.dart';
import 'package:zhaoxiaowu_app/models/accounting_detail.dart';
import 'package:zhaoxiaowu_app/models/accounting_info.dart';
import 'package:zhaoxiaowu_app/viewmodels/accounting_viewmodel.dart';

class AccountingItem extends StatelessWidget {
  const AccountingItem(this.data);

  final AccountingInfo data;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        _getTopWidget(context),
        SizedBox(height: 8.0),
        Divider(height: 1.0),
        SizedBox(height: 8.0),
        _getBottomWidget(),
      ],
    );
  }

  List<Widget> _getBottomChildren() {
    List<Widget> widgets = [];
    var datas = data.data ?? [];
    for (var i = 0; i < datas.length; i++) {
      AccountingDetail model = datas[i];
      var widget = CupertinoLeftScroll(
        key: Key(model.id.toString()),
        closeTag: LeftScrollCloseTag('记账tag'),
        opacityChange: true,
        bounce: true,
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            children: [
              Icon(Icons.account_balance_wallet),
              SizedBox(width: 16),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(model.action == 0 ? "收入" : "支出"),
                    Text(
                      "描述：" + model.desc.toString(),
                      style: TextStyle(color: Colors.grey),
                    ),
                  ],
                ),
              ),
              Text(model.money.toString()),
            ],
          ),
        ),
        buttons: [
          LeftScrollItem(
            text: "删除",
            color: Colors.red,
            onTap: () {
              navigatorKey.currentContext?.read<AccountingViewModel>().delete(model.id.toString());
            },
          ),
        ],
      );
      widgets.add(widget);
      widgets.add(SizedBox(width: 16));
    }
    return widgets;
  }

  Widget _getBottomWidget() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: _getBottomChildren(),
    );
  }

  Widget _getTopWidget(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Row(
        children: [
          Expanded(
            child: Text(
              data.date.toString(),
              style: Theme.of(context).textTheme.headline6,
            ),
          ),
          Text(
            "收入：" + data.income.toString(),
            style: Theme.of(context).textTheme.subtitle1,
          ),
          SizedBox(width: 8),
          Text(
            "支出：" + data.expenditure.toString(),
            style: Theme.of(context).textTheme.subtitle1,
          ),
        ],
      ),
    );
  }
}
